package com.lcw.library.imagepicker.view.addimgview;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.lcw.library.imagepicker.R;
import com.lcw.library.imagepicker.data.MediaFile;
import com.lcw.library.imagepicker.manager.ConfigManager;
import com.lcw.library.imagepicker.utils.MediaFileUtil;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.DrawableRes;
import androidx.annotation.LayoutRes;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;


public class AddImageAdapter extends RecyclerView.Adapter<AddImageAdapter.ViewHolder> {

    private Context mContext;
    private OnImageChangeListener listener;
    private List<MediaFile> mMediaFileList;
    private MediaFile addItem;
    private int maxCount;
    private int layoutRes;
    private int delImgResId;


    public AddImageAdapter(Context context,
            List<MediaFile> mediaFolderList,
            @DrawableRes int addImgResId,
            int maxCount,
            @LayoutRes int layoutRes,
            @DrawableRes int delImgResId) {
        this.mContext = context;
        if (mediaFolderList == null) {
            this.mMediaFileList = new ArrayList<>();
        } else {
            this.mMediaFileList = mediaFolderList;
        }
        addItem = new MediaFile(true, addImgResId);
        this.maxCount = maxCount;
        this.layoutRes = layoutRes;
        this.delImgResId = delImgResId;
        sortListData();
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new ViewHolder(LayoutInflater.from(mContext).inflate(layoutRes, null));
    }

    @Override
    public void onBindViewHolder(@NonNull final ViewHolder holder, final int position) {
        final MediaFile mediaFile = mMediaFileList.get(position);
        holder.ivBtnDelete.setImageResource(delImgResId);
        if (mediaFile.isAdd()) {
            holder.ivBtnDelete.setVisibility(View.GONE);
            holder.ivIsVideo.setVisibility(View.GONE);
            try {
                ConfigManager.getInstance().getImageLoader().loadImage(holder.ivPicker, mediaFile.getResId());
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else {
            holder.ivBtnDelete.setVisibility(View.VISIBLE);
            if (MediaFileUtil.isVideoFileType(mediaFile.getPath())) {
                holder.ivIsVideo.setVisibility(View.VISIBLE);
            } else {
                holder.ivIsVideo.setVisibility(View.GONE);
            }
            try {
                ConfigManager.getInstance().getImageLoader().loadImage(holder.ivPicker, mediaFile.getPath());
            } catch (Exception e) {
                e.printStackTrace();
            }

        }
        holder.ivPicker.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listener != null) {
                    listener.onAddBtnClick(holder.getAdapterPosition(), mediaFile.isAdd());
                }
            }
        });
        holder.ivBtnDelete.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                mMediaFileList.remove(mediaFile);
                sortListData();
                notifyDataSetChanged();
                if (listener != null) {
                    listener.onDelete(mediaFile);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return mMediaFileList == null?0:mMediaFileList.size();
    }

    public void sortListData() {
        mMediaFileList.remove(addItem);
        if (mMediaFileList.size() < maxCount) {
            mMediaFileList.add(addItem);
        }
    }

    class ViewHolder extends RecyclerView.ViewHolder {

        private ImageView ivPicker;
        private ImageView ivBtnDelete;
        private ImageView ivIsVideo;

        public ViewHolder(View itemView) {
            super(itemView);
            ivPicker = itemView.findViewById(R.id.iv_picker);
            ivBtnDelete = itemView.findViewById(R.id.iv_btn_delete);
            ivIsVideo = itemView.findViewById(R.id.iv_is_video);
        }
    }

    public int getMaxCount() {
        return maxCount;
    }

    public void setMaxCount(int maxCount) {
        this.maxCount = maxCount;
    }

    public void setOnImageChangeListener(OnImageChangeListener clickListener) {
        this.listener = clickListener;
    }

    public interface OnImageChangeListener {
        void onAddBtnClick(int position, boolean isAdd);

        void onDelete(MediaFile item);

//        void showBigImages(List<MediaFile> mMediaFileList, int adapterPosition);
    }
}
